﻿using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine.SceneManagement;

namespace GameFramework.Scene
{
    /// <summary>
    /// 场景服务拓展方法
    /// </summary>
    public static class SceneServiceExtensions
    {
        /// <summary>
        /// 以Additive模式加载指定场景 若token被取消则卸载加载的场景资源
        /// </summary>
        public static async UniTask<IScene> LoadAdditiveSceneAsync(this ISceneService service, string assetKey, bool activateOnLoad = true, int priority = 100, CancellationToken token = default)
        {
            var scene = await service.LoadSceneAsync(assetKey, LoadSceneMode.Additive, false, priority);
            if (scene == null)
                return null;

            if (token.IsCancellationRequested)
            {
                scene.Unload();
                return null;
            }

            if (activateOnLoad)
                scene.Activate();
            
            return scene;
        }
    }
}